package cn.itcast.dao.impl;

import cn.itcast.dao.StudentDao;
import cn.itcast.domain.Student;
import cn.itcast.utils.JDBCUtil;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {

    private Connection conn = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    //查询所有学生信息
    public List<Student> findall() {
        List<Student> lis = new ArrayList<Student>();

        try {
            //1.获取连接
            conn = JDBCUtil.getConn();
            //2.编写sql
            String sql = "select * from student";
            //3.创建预编译对象获取结果集
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

            //4.遍历机结果集
           while (rs.next()){
               Student stu = new Student(rs.getInt(1),rs.getString(2),rs.getInt(3),rs.getDate(4));
               lis.add(stu);
           }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //5.释放资源
            JDBCUtil.close(conn,ps,rs);
        }


        return lis;
    }
}
